<%@page import="net.groupme.user.UserManager"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.Group"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="java.util.Collections"%>
<%@page import="net.groupme.model.dao.DATABASE_CONSTANTS"%>
<%@page import="net.groupme.model.Tag"%>
<%@page import="java.util.Collection"%>
<div class="grouplistHead">
	<div class="groupListHeadMenu">
		sort by: 
		<a href="./<%=VIEW_VOCABULARY.ACTION_HOME%>?<%=VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW %>=<%=VIEW_VOCABULARY.VIEW_SUBSCRIPTIONS %>
				&<%=VIEW_VOCABULARY.PARAMETER_SORT %>=<%=VIEW_VOCABULARY.SORT_ALPHA_ASC%>">abc.. </a>
		|<a href="./<%=VIEW_VOCABULARY.ACTION_HOME%>?<%=VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW %>=<%=VIEW_VOCABULARY.VIEW_SUBSCRIPTIONS %>
				&<%=VIEW_VOCABULARY.PARAMETER_SORT %>=<%=VIEW_VOCABULARY.SORT_ALPHA_DESC%>">zyx.. </a>
		|<a href="./<%=VIEW_VOCABULARY.ACTION_HOME%>?<%=VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW %>=<%=VIEW_VOCABULARY.VIEW_SUBSCRIPTIONS %>
				&<%=VIEW_VOCABULARY.PARAMETER_SORT %>=<%=VIEW_VOCABULARY.SORT_TAD_ASC%>">old..new </a>
		|<a href="./<%=VIEW_VOCABULARY.ACTION_HOME%>?<%=VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW %>=<%=VIEW_VOCABULARY.VIEW_SUBSCRIPTIONS %>
				&<%=VIEW_VOCABULARY.PARAMETER_SORT %>=<%=VIEW_VOCABULARY.SORT_TAD_DESC%>">new..old </a>
	</div>
	<h3>Your Subscriptions</h3>
</div>
<% UserManager um = (UserManager) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
						.getBean(VIEW_VOCABULARY.USER_MANGER, UserManager.class); %>
<div id="mainGroupwindowContent">
		<% 
		List<Group> subscriptions;
		if (VIEW_VOCABULARY.SORT_ALPHA_ASC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			subscriptions = um.getSubscribedGroupsOfUserOrderedBy(DATABASE_CONSTANTS.RESOURCE_NAME);
		}
		else if (VIEW_VOCABULARY.SORT_ALPHA_DESC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			subscriptions = um.getSubscribedGroupsOfUserOrderedBy(DATABASE_CONSTANTS.RESOURCE_NAME);
			Collections.reverse(subscriptions);
		}
		else if (VIEW_VOCABULARY.SORT_TAD_ASC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			subscriptions = um.getSubscribedGroupsOfUserOrderedBy(DATABASE_CONSTANTS.TABLE_USERGROUP + "." + DATABASE_CONSTANTS.USER_GROUP_TAD);
		}
		else {
			subscriptions = um.getSubscribedGroupsOfUserOrderedBy(DATABASE_CONSTANTS.TABLE_USERGROUP + "." + DATABASE_CONSTANTS.USER_GROUP_TAD);
			Collections.reverse(subscriptions);
		}
		Iterator it = subscriptions.iterator();
		if (!it.hasNext()) { %>
			- You have no Subscriptions -
		<% }
		else { %>
			<%@include file="list-groups.jsp"%>	
	<%	}%>
</div>
